package com.ruoyi.project.system.video.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.system.video.domain.Comment;
import com.ruoyi.project.system.video.mapper.VideoMapper;
import com.ruoyi.project.system.video.domain.Video;
import com.ruoyi.project.system.video.service.IVideoService;
import com.ruoyi.common.utils.text.Convert;

/**
 * videoService业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-04
 */
@Service
public class VideoServiceImpl implements IVideoService 
{
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 查询video
     * 
     * @param id video主键
     * @return video
     */
    @Override
    public Video selectVideoById(Long id)
    {
        return videoMapper.selectVideoById(id);
    }

    /**
     * 查询video列表
     * 
     * @param video video
     * @return video
     */
    @Override
    public List<Video> selectVideoList(Video video)
    {
        return videoMapper.selectVideoList(video);
    }

    /**
     * 新增video
     * 
     * @param video video
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVideo(Video video)
    {
        int rows = videoMapper.insertVideo(video);
        insertComment(video);
        return rows;
    }

    /**
     * 修改video
     * 
     * @param video video
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVideo(Video video)
    {
        videoMapper.deleteCommentById(video.getId());
        insertComment(video);
        return videoMapper.updateVideo(video);
    }

    /**
     * 批量删除video
     * 
     * @param ids 需要删除的video主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVideoByIds(String ids)
    {
        videoMapper.deleteCommentByIds(Convert.toStrArray(ids));
        return videoMapper.deleteVideoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除video信息
     * 
     * @param id video主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVideoById(Long id)
    {
        videoMapper.deleteCommentById(id);
        return videoMapper.deleteVideoById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param video video对象
     */
    public void insertComment(Video video)
    {
        List<Comment> commentList = video.getCommentList();
        Long id = video.getId();
        if (StringUtils.isNotNull(commentList))
        {
            List<Comment> list = new ArrayList<Comment>();
            for (Comment comment : commentList)
            {
                comment.setId(id);
                list.add(comment);
            }
            if (list.size() > 0)
            {
                videoMapper.batchComment(list);
            }
        }
    }
}
